package com.qingpeng.pay.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.qingpeng.pay.R;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.LoginBean;
import com.qingpeng.pay.beans.MoneyBean;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.APP;
import com.qingpeng.pay.utils.StringUtils;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.BaseNumberCodeView;
import com.qingpeng.pay.views.BottomSheetNumberCodeView;
import com.qingpeng.pay.views.BottomSheetNumberCodeViewActivity;
import com.qingpeng.pay.views.TitleBar;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WithdrawCashActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_yue)
    TextView tvYue;
    @BindView(R.id.btn_ok)
    Button btnOk;
    @BindView(R.id.bsn_pwd)
    BottomSheetNumberCodeView bsnPwd;
    private MaterialDialog alertDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WithdrawCashActivity.this.finish();
            }
        });
        tvYue.setText(String.format(getResources().getString(R.string.activity_cash), "0"));
        bsnPwd.setIsPassword(true);
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        Call<MoneyBean> getaccount = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).getaccount();
        getaccount.enqueue(new Callback<MoneyBean>() {
            @Override
            public void onResponse(Call<MoneyBean> call, Response<MoneyBean> response) {
                MoneyBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(WithdrawCashActivity.this, body.getInfo());
                    return;
                }
                tvYue.setText(String.format(getResources().getString(R.string.activity_cash),body.getData().getTolscore()+""));
            }

            @Override
            public void onFailure(Call<MoneyBean> call, Throwable t) {
                ToastUtils.showToast(WithdrawCashActivity.this, "网络请求错误,请稍后重试");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @OnClick(R.id.btn_ok)
    public void onViewClicked() {
        if (!checkTime()) {
            ToastUtils.showToast(WithdrawCashActivity.this,"请在每天10点-22点之间提现");
            return;
        }
        String s = etMoney.getText().toString();
        if (StringUtils.isEmpty(s)) {
            ToastUtils.showToast(WithdrawCashActivity.this,"请输入提现金额");
            return;
        }
        if (APP.userInfo != null && StringUtils.isEmpty(APP.userInfo.getWx_name())) {
            showToastBindDialog(1);
        } else if (APP.userInfo != null && StringUtils.isEmpty(APP.userInfo.getZf_pwd())) {
            showToastBindDialog(2);
        } else {
            Call<NullBean> nullBeanCall = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).withdrawCheck(etMoney.getText().toString());
            nullBeanCall.enqueue(new Callback<NullBean>() {
                @Override
                public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                    bsnPwd.setVisibility(View.VISIBLE);
                    btnOk.setVisibility(View.GONE);
                    bsnPwd.showToastNumberCodeLayout();
                    bsnPwd.setOnHideBottomLayoutListener(new BottomSheetNumberCodeView.OnHideBottomLayoutListener() {
                        @Override
                        public void onHide() {
                            bsnPwd.setVisibility(View.GONE);
                            btnOk.setVisibility(View.VISIBLE);
                            hiddenKeyboard();
                        }
                    });
                    bsnPwd.setNumberCodeCallback(new BaseNumberCodeView.OnInputNumberCodeCallback() {
                        @Override
                        public void onResult(String code) {
                            if (StringUtils.isEmpty(code))
                                return;
                            Call<NullBean> tx = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).tx(etMoney.getText().toString(), Integer.parseInt(code));
                            tx.enqueue(new Callback<NullBean>() {
                                @Override
                                public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                                    NullBean body = response.body();
                                    if (body.getStatus() != 1) {
                                        ToastUtils.showToast(WithdrawCashActivity.this,body.getInfo());
                                        return;
                                    }
                                    WithdrawCashActivity.this.finish();
                                    ToastUtils.showToast(WithdrawCashActivity.this,body.getInfo());
                                }

                                @Override
                                public void onFailure(Call<NullBean> call, Throwable t) {
                                    ToastUtils.showToast(WithdrawCashActivity.this,"网络请求错误,请稍后重试");
                                }
                            });
                        }
                    });
                }

                @Override
                public void onFailure(Call<NullBean> call, Throwable t) {
                    ToastUtils.showToast(WithdrawCashActivity.this, "网络请求错误,请稍后重试");
                }
            });

        }
    }

    private void showToastBindDialog(final int type) {
        if (alertDialog == null) {
            View inflate = View.inflate(this, R.layout.dialog_alert, null);
            TextView tvCansle = (TextView) inflate.findViewById(R.id.tv_cansle);
            TextView tvTx = (TextView) inflate.findViewById(R.id.tv_tx);
            if (type == 1)
                tvTx.setText("未实名认证，是否去认证?");
            else
                tvTx.setText("未设置支付密码，是否去设置?");
            TextView tvOk = (TextView) inflate.findViewById(R.id.tv_ok);
            tvCansle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (alertDialog != null) {
                        alertDialog.dismiss();
                    }
                }
            });
            tvOk.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (alertDialog != null) {
                        alertDialog.dismiss();
                    }
//                        startActivity(BindActivity.class);
                    if (type == 1) {
                        startActivity(UserInfoActivity.class);
                        finish();
                    } else {
                        startActivity(VerifyCodeActivity.class);
                        finish();
                    }
                }
            });
            alertDialog = new MaterialDialog.Builder(this).customView(inflate, false).show();
        } else {
            if (alertDialog.isShowing()) {
                alertDialog.dismiss();
            } else {
                alertDialog.show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    public boolean checkTime() {
        Calendar cal = Calendar.getInstance();// 当前日期
        int hour = cal.get(Calendar.HOUR_OF_DAY);// 获取小时
        int minute = cal.get(Calendar.MINUTE);// 获取分钟
        int minuteOfDay = hour * 60 + minute;// 从0:00分开是到目前为止的分钟数
        final int start = 10 * 60;// 起始时间 00:20的分钟数
        final int end = 22 * 60;// 结束时间 8:00的分钟数
        if (minuteOfDay >= start && minuteOfDay <= end) {
            return true;
        } else {
            return false;
        }
    }
}
